package com.jk.controller.admin;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.jk.pojo.Industry;
import com.jk.pojo.User;
import com.jk.service.IndustryService;

/**
 *
 * @author jdz
 *
 */
@Controller
@RequestMapping("/admin/industry/")
public class IndustryController {
private static final String JSP_PATH = "/industry/";

	@Resource
	private IndustryService industryService;

	/**行业信息管理列表
	 * @param request
	 * @param modelMap
	 * @return
	 */
	@RequestMapping("list.do")
	public String query(HttpServletRequest request,ModelMap modelMap){
		try {
			request.getParameter("");
			List<Industry> varList = industryService.queryAll();
			if(modelMap != null){
				modelMap.addAttribute("varList",varList);
	        }

		} catch (Exception e) {
			e.printStackTrace();
		}
		return JSP_PATH+"industry-list";
	}

	/**行业信息管理列表
	 * @param request
	 * @param modelMap
	 * @return
	 */
	@RequestMapping("listByCondition.do")
	public String listByCondition(HttpServletRequest request, ModelMap modelMap){
		try {
			modelMap.clear();
			String searchText = String.valueOf(request.getParameter("searchText"));
			if(modelMap != null){
				modelMap.put("searchText", searchText);
			}

			List<Industry> varList = industryService.listByCondition(request, modelMap);
			if(modelMap != null){
				modelMap.put("varList", varList);
	        }

		} catch (Exception e) {
			e.printStackTrace();
		}
		return JSP_PATH+"industry-list";
	}

	/**跳转向新建行业的页面
	 * @param request
	 * @param modelMap
	 * @return
	 */
	@RequestMapping("toAdd.do")
	public String toAddPage(HttpServletRequest request,ModelMap modelMap){
		return JSP_PATH+"industry-add";
	}

	/**
	 * @param request
	 * @param modelMap
	 * @return
	 */
	@ResponseBody
	@RequestMapping("add.do")
	public Map addNewIndurstry(HttpServletRequest request,ModelMap modelMap){
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			String name = String.valueOf(request.getParameter("name"));
			String	description = String.valueOf(request.getParameter("description"));
			String strType = String.valueOf(request.getParameter("type"));
			int	type = ("").equals(strType)||("null").equalsIgnoreCase(strType) ? 0:Integer.parseInt(strType);

			Industry record = new Industry();
			record.setName(name);
			record.setDescription(description);
			record.setType(type);
			industryService.save(record);

			map.put("flag", true);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return map;
	}

	/**行业信息管理列表
	 * @param request
	 * @param modelMap
	 * @return
	 */
	@RequestMapping("update.do")
	public String updateIndustry(HttpServletRequest request,ModelMap modelMap){
		try {
			int id = 0;
			String name = "";
			String description = "";
			int type = 0;
			if(modelMap != null){
				name = String.valueOf(request.getParameter("name"));
				description = String.valueOf(request.getParameter("description"));
				String strType = String.valueOf(request.getParameter("type"));
				type = ("").equals(strType)||("null").equalsIgnoreCase(strType) ? 0:Integer.parseInt(strType);
	        }

		} catch (Exception e) {
			e.printStackTrace();
		}
		return JSP_PATH+"industry-list";
	}

	/**行业信息管理删除
	 * @param request
	 * @param modelMap
	 * @return
	 */
	@RequestMapping("delete.do")
	public String deleteIndustry(HttpServletRequest request,ModelMap modelMap){
		try {
			List<Industry> industry_list = industryService.queryAll();
			if(modelMap != null){
				modelMap.addAttribute("industry_list",industry_list);
	        }

		} catch (Exception e) {
			e.printStackTrace();
		}
		return JSP_PATH+"industry-list";
	}

	/**行业信息管理删除
	 * @param request
	 * @param modelMap
	 * @return
	 */
	@ResponseBody
	@RequestMapping("batchDelete.do")
	public Map batchDeleteIndustry(HttpServletRequest request,ModelMap modelMap){
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			String param = String.valueOf(request.getParameter("deleteIds"));
			String deleteIds = (("").equals(param) || ("null").equalsIgnoreCase(param)) ? "0":param;

			List<Object> list = new ArrayList<Object>();
			String[] array = deleteIds.split(",");
			for(String id : array){
				list.add(Integer.parseInt(id));
			}

			int returnId = industryService.deleteByIds(Industry.class, "id", list);

			map.put("flag", true);
			map.put("returnId", returnId);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return map;
	}


}
